/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

void 
readFed();

int
main (int argc, char *argv[])
{
  readFed();
  NS_LOG_UNCOND("Hello World!!!");  
      
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

  NodeContainer nodes;
  nodes.Create (2);

  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);

  InternetStackHelper stack;
  stack.Install (nodes);

  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}
/*void error(const char *msg)
{
    perror(msg);
    exit(1);
}*/

void readFed()
{

     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n,count=0;
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
  NS_LOG_UNCOND("Hello World!!!");   
        //error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = 2201;
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0)
  NS_LOG_UNCOND("Hello World!!!");   
              //error("ERROR on binding");
     listen(sockfd,5);


  clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          //error("ERROR on accept");
  NS_LOG_UNCOND("Hello World!!!");  

     while(count<21)
     {
       count++;
       //bzero(buffer,256);
//n = write(newsockfd,"^***",1);
 //    if (n < 0) error("ERROR writing to socket");
	n=read(newsockfd,buffer,256);
   if(n>0);
       		//printf("%s\n",buffer);
  NS_LOG_UNCOND("Hello Worldhhhhhh!!!");  
       if (n < 0)  NS_LOG_UNCOND("Hello World!!!");   //error("ERROR reading from socket");
 
       //if(strcmp(buffer,"*****")==0);
	//	break;
     }
    
     close(newsockfd);
     close(sockfd);
 //    printf("%d",count);     
//return 0; 
}
